﻿using APS168_W64;
using PerpheralDevice.Units;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PerpheralDevice.LeadShine;
/// <summary>
/// 雷赛DMC板卡 输入开关(应用只读开关)
/// </summary>
public class AdLinkSwitchRO :SwitchRO {

    private ushort mfCardNO;
    private ushort mfMacAddr;
    public override string UnitID { get; }
    public AdLinkSwitchRO(PerpDev perpDev, int macAddr) {
        this.PerpDev = perpDev;
        this.UnitID = macAddr.ToString();
    }

    public override SwitchRO View() {
        
        if (this.PerpDev.IsMocking) {
            this.GenMockValue();            
            return this;
        }

        int tempDiState = 0;
        APS168.APS_read_d_input(this.mfCardNO, 0, ref tempDiState);
        // 低电平有效
        bool tempIsOpen = (tempDiState & (1 << this.mfMacAddr)) == 0;
        if (tempIsOpen != this.IsOpen) {
            this.IsOpen = tempIsOpen;
            this.OnStatusChangedRaise();
        }

        return this;
    }

}
